//尝试将a_temp = a_temp + min_l;和b_temp = b_temp + min_l * ldb;融合进内核计算的参数中
//但是这样好像会增加计算量

#include <stdio.h>
//#include "gemm_ukernel_header.h"
extern int sgemm_ukernel_8_8_128b(int _m, int _n, int _k, float alpha, float* _a, float* _b, float* _c, int ldc, float beta, int ldb);

#define GEMM_P 128   //m
//#define GEMM_P 12288
#define GEMM_Q 240   //k
//#define GEMM_Q 240    //My_Para
#define GEMM_R 12288 //n
//#define GEMM_R 128   //My_Para
#define GEMM_UNROLL_M 8
#define GEMM_UNROLL_N 8

int sgemm_mNK(int m, int n, int k, float alpha, float *a, int lda, float *b, int ldb, float beta, float *c, int ldc){

    int min_i = 0, min_l = 0, min_j = 0;
    int js=0, ls=0, is=0;
    int jjs=0, min_jj = 0;

    // //int min_js = 0;         //新增记录 N 维度增量
    // float *b_temp2 = b;  //用于kernel2 b地址更新
    // float *c_temp2 = c;

    int l1stride;
    float *a_temp = a;
    float *b_temp = b;
    float *c_temp = c;

    for(js = 0; js < n; js += GEMM_R){
        min_j = n - js;
        if (min_j > GEMM_R) min_j = GEMM_R;
        for(ls = 0; ls < k; ls += min_l){

            min_l = k - ls;

            if (min_l >= GEMM_Q * 2) {

	            min_l  = GEMM_Q;
            } else {
	        if (min_l > GEMM_Q) {
	            min_l = ((min_l / 2 + GEMM_UNROLL_M - 1)/GEMM_UNROLL_M) * GEMM_UNROLL_M;
	                            }
            }

            min_i = m;
            l1stride = 1;
            if (min_i >= GEMM_P * 2) {
	            min_i = GEMM_P;
            } else {
	        if (min_i > GEMM_P) {
	            min_i = ((min_i / 2 + GEMM_UNROLL_M - 1)/GEMM_UNROLL_M) * GEMM_UNROLL_M;
	        } else {
	        l1stride = 0;
	                }
                    }
    
            for(jjs = js; jjs < js + min_j; jjs += min_jj){
	min_jj = min_j + js - jjs;
    if (min_jj >= 3*GEMM_UNROLL_N) min_jj = 3*GEMM_UNROLL_N; //循环展开大小
        else if (min_jj > GEMM_UNROLL_N) min_jj = GEMM_UNROLL_N;

        // if (min_jj >= 16*GEMM_UNROLL_N) min_jj = 16*GEMM_UNROLL_N; //My-try
        // else if (min_jj > 8*GEMM_UNROLL_N) min_jj = 8*GEMM_UNROLL_N;
        // else if (min_jj > 4*GEMM_UNROLL_N) min_jj = 4*GEMM_UNROLL_N;
        // else if (min_jj > GEMM_UNROLL_N) min_jj = GEMM_UNROLL_N;
        
        //printf("Kernel1 a_temp: %p\n", (void*)(a_temp));
        sgemm_ukernel_8_8_128b(min_i, min_jj, min_l, alpha, a_temp, (float *)(b_temp) + jjs, (float *)(c_temp) + jjs, lda, beta, ldb);
            }                                                                                      
                                              
  for(is =  min_i; is < m; is += min_i){
	min_i = m - is;

	if (min_i >= GEMM_P * 2) {
	  min_i = GEMM_P;
	} else
	  if (min_i > GEMM_P) {
	    min_i = ((min_i / 2 + GEMM_UNROLL_M - 1)/GEMM_UNROLL_M) * GEMM_UNROLL_M;
	  }

    //printf("Kernel 2  a_temp+is*lda:%p\n", (void*)(a_temp+is*lda));
    sgemm_ukernel_8_8_128b(min_i, min_j, min_l, alpha, (float *)(a_temp) + is * lda, (float *)(b_temp) + js, (float *)(c_temp) + is * ldc + js, lda, beta, ldb);
    
}

    a_temp = a_temp + min_l;
    b_temp = b_temp + min_l * ldb;
  }

  a_temp = a;
  b_temp = b; 
}

return 0;
}